// pages/history/history.js
const utils = require('../../utils/util.js');

Page({
  data: {
    HistoryList: [],
    PlayList: [],
    PlayListVisible: false
  },

  onLoad(options) {
    this.setData({
      HistoryList: wx.getStorageSync('HistoryList'),
      PlayList: wx.getStorageSync('MusicFromData')
    });
    
  },

  onShow() {
    this.setData({
      HistoryList: wx.getStorageSync('HistoryList'),
      PlayList: wx.getStorageSync('MusicFromData')
    });
  },

  //添加至歌单
  PlayListAdd:function(e) {
    let id = e.currentTarget.dataset.sId;
    let selectedSong = this.data.HistoryList.find(item => item.songMid === id);
    
    if (selectedSong) {
      if(!selectedSong.isAdded) {
        utils.addToPlaylist(selectedSong.songMid);
        console.log("添加成功");
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 1000
        });
      } else {
        console.log("歌曲已存在歌单中");
        wx.showToast({
          title:'请勿重复添加',
          icon: 'none',
          duration: 1000
        })
      }
    } 
  },

  ChangeLoveList:function(e) {
    let id = e.currentTarget.dataset.sId;
    utils.LovelistChange(id);
    this.triggerEvent('LovelistSongChange', { sId: id });
    console.log("改变收藏夹成功");
  },

  OpenPlayList:function() {
    this.setData({
      PlayListVisible: true
    });
  },
  ClosePlayList:function() {
    this.setData({
      PlayListVisible: false
    });
  },
})